package gui.Machine;

import gui.AppKoopy;
import gui.PrinterDialog;
import gui.RepairType.RepairTypeListFrame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;

import model.*;
import service.*;

/**
 * @author Osahon Idehen
 */
public class MachineListFrame extends JFrame {
	private JLabel Machine, ID, Name, MachineType, Reparation, Description,
			pic;
	private JScrollPane scpMachine;
	private JList lstMachine;
	private JTextArea textArea;
	private JPanel pSouth;
	private JScrollPane scrollPane;
	private JTextField txfID, txfName, txfMachineType, txfReparation,
			txfDescription, Info, Search;
	private JButton Create, Update, Delete, printer, printAll;
	public boolean modalResult;

	private Controller controller = new Controller();

	protected JTable m_table;
	protected MJTable_StockTableData m_data;

	public MachineListFrame() {
		setTitle("Machines");
		setBounds(200, 200, 600, 300);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());

		getTable();
		getButtons();
		getTextArea();
		getSearch();
	}

	private void getTable() {
		UIManager.put("Table.focusCellHighlightBorder", new LineBorder(
				Color.black, 0));

		m_data = new MJTable_StockTableData();
		m_table = new JTable();
		m_table.setAutoCreateColumnsFromModel(false);
		m_table.setModel(m_data);
		for (int k = 0; k < m_data.getColumnCount(); k++) {
			DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
			renderer
					.setHorizontalAlignment(MJTable_StockTableData.m_columns[k].m_alignment);
			TableColumn column = new TableColumn(k,
					MJTable_StockTableData.m_columns[k].m_width, renderer, null);
			m_table.addColumn(column);
		}
		JTableHeader header = m_table.getTableHeader();
		header.setUpdateTableInRealTime(false);
		JScrollPane ps = new JScrollPane();
		ps.getViewport().setBackground(m_table.getBackground());
		ps.getViewport().add(m_table);
		getContentPane().add(ps, BorderLayout.CENTER);
	}

	private void getButtons() {
		pSouth = new JPanel(new BorderLayout());
		getContentPane().add(pSouth, BorderLayout.SOUTH);
		
		
		JPanel pButtons = new JPanel();
		pSouth.add(pButtons, BorderLayout.CENTER);

		Create = new JButton("Create");
		Create.addActionListener(controller);
		pButtons.add(Create);

		Update = new JButton("Update");
		Update.addActionListener(controller);
		pButtons.add(Update);

		Delete = new JButton("Delete");
		Delete.addActionListener(controller);
		pButtons.add(Delete);
	}

	private void getTextArea() {
		textArea = new JTextArea("Descriprion:");
		textArea.setFocusable(true);
		textArea.setEditable(false);
		textArea.setWrapStyleWord(true);
		textArea.setLineWrap(true);

		scrollPane = new JScrollPane(textArea);
		pSouth.add(scrollPane, BorderLayout.NORTH);
	}

	private void getSearch() {
		JPanel s = new JPanel(new FlowLayout(FlowLayout.LEADING));
		pic = new JLabel(AppKoopy.createImageIcon("images/search.png"));
		pic.setPreferredSize(new Dimension(20, 22));
		s.add(pic);
		Search = new JTextField(" search");
		Search.setPreferredSize(new Dimension(160, 20));
		s.add(Search);

		pSouth.add(s, BorderLayout.SOUTH);
	}

	private void getDescription(List<Machine> list) {
		if (m_table.getSelectedRow() == -1) {
			return;
		}
		int index = m_table.getSelectedRow();
		Machine anime = (Machine) list.get(index);
		textArea.setText(anime.getDescription());
	}

	private List<Machine> m_list;

	public void fillListMachine() {
		m_list = new ArrayList<Machine>();

		m_list = new ArrayList<Machine>();

		GregorianCalendar gc = new GregorianCalendar(2008,
				GregorianCalendar.JANUARY, 9, 14, 12, 42);
		// -----------------------------------
		MachineType mt = new MachineType(2345678, "SP3000", "");
		MachineType mt1 = new MachineType(2345678, "SP3000", "");
		// -----------------------------------
		Machine m = new Machine(1234567, "Machine01", "", mt);
		// -----------------------------------
		Reparation r1 = new Reparation(3132123, gc, "", m);
		// -----------------------------------
		SparePart sp = new SparePart(2203459, 331, 311, 321, "SpR1", 3, 32, 7,
				"for GS20");
		SparePart sp1 = new SparePart(1111111, 111, 111, 000, "ring26R1", 123,
				120, 25, "for GS20");
		SparePart sp2 = new SparePart(2202334, 331, 311, 321, "Sp2R1", 30, 32,
				7, "for GS32");

		sp.createSparePartHistory(r1, new SparePartUse(3, sp));
		sp1.createSparePartHistory(null, new SparePartUse(2, sp1));
		sp2.createSparePartHistory(r1, new SparePartUse(7, sp2));
		// -----------------------------------
		r1.endReparation(new GregorianCalendar(2008, GregorianCalendar.MAY, 10,
				18, 14, 32));
		// -----------------------------------
		GregorianCalendar gc2 = new GregorianCalendar(2008,
				GregorianCalendar.MAY, 12, 14, 12, 42);
		Reparation r2 = new Reparation(3132132, gc2, "", m);
		// -----------------------------------
		SparePart sp21 = new SparePart(2203459, 331, 311, 321, "SpR2", 3, 32,
				7, "for GS20");
		SparePart sp122 = new SparePart(1111111, 111, 111, 000, "ring26R2",
				123, 120, 25, "for GS20");
		SparePart sp223 = new SparePart(2202334, 331, 311, 321, "Sp2R2", 30,
				32, 7, "for GS32");

		sp21.createSparePartHistory(r2, new SparePartUse(12, sp21));
		sp122.createSparePartHistory(r2, new SparePartUse(4, sp122));
		sp223.createSparePartHistory(r2, new SparePartUse(2, sp223));
		m_list.clear();

		// ------------------------------------------------------

		Machine m2 = new Machine(1000001, "GS2", "Straight machine", mt);
		Machine m3 = new Machine(1000002, "GS3", "Triangle machine", mt1);

		m2.addReparation(new Reparation(2020202, gc, "", m));
		m3.addReparation(new Reparation(2020202, gc, "", m2));

		m_list.add(m2);
		m_list.add(m3);

		m_data.setDefaultData(m_list);
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == Create) {
				MachineCUDialog macRH = new MachineCUDialog(
						MachineListFrame.this, "Create a Machine");
				macRH.setVisible(true);
			}

			if (e.getSource() == printer) {
				if (m_table.getSelectedRow() == -1) {
					return;
				}
				int index = m_table.getSelectedRow();
				Machine machine = (Machine) m_list.get(index);

				PrinterDialog RH = new PrinterDialog(MachineListFrame.this,
						"-<<Machine(" + machine.getName() + ") Status>>-", "HI");
				RH.setVisible(true);
			}
			
			
				if (e.getSource() == Update) {
					MachineCUDialog macRH = new MachineCUDialog(
							MachineListFrame.this, "Update a Machine");
					macRH.setVisible(true);
				}

				if (e.getSource() == printer) {
					if (m_table.getSelectedRow() == -1) {
						return;
					}
					int index = m_table.getSelectedRow();
					Machine machine = (Machine) m_list.get(index);

					PrinterDialog RH = new PrinterDialog(MachineListFrame.this,
							"-<<Machine(" + machine.getName() + ") Status>>-", "HI");
					RH.setVisible(true);
				}

				if (e.getSource() == Delete) {
					MachineCUDialog macRH = new MachineCUDialog(
							MachineListFrame.this, "Delete a Machine");
					macRH.setVisible(true);
				}

				if (e.getSource() == printer) {
					if (m_table.getSelectedRow() == -1) {
						return;
					}
					int index = m_table.getSelectedRow();
					Machine machine = (Machine) m_list.get(index);

					PrinterDialog RH = new PrinterDialog(MachineListFrame.this,
							"-<<Machine(" + machine.getName() + ") Status>>-", "HI");
					RH.setVisible(true);
				}

			if (e.getSource() == printAll) {
				String allText = "";
				// TODO
				for (@SuppressWarnings("unused")
				Machine o : m_list) {

				}

				PrinterDialog RH = new PrinterDialog(MachineListFrame.this,
						"reparation from Machine", allText);
				RH.setVisible(true);
			}
		}
	}
}
